﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace Utility
{
    public class StringUtil
    {
        #region 返回全球唯一索引
        /// <summary>
        /// 静态方法：返回全球唯一索引
        /// </summary>
        /// <returns></returns>
        public static Guid NewGuid()
        {
            return Guid.NewGuid();
        }
        #endregion

        #region 转为小写字符串
        /// <summary>
        /// 方法（静态），转为小写字符串
        /// </summary>
        /// <param name="strInput">原字符串</param>
        /// <returns>目的字符串</returns>
        public static string ToLower(string strInput)
        {
            return strInput.ToLower();
        }
        #endregion

        #region 转为大写字符串
        /// <summary>
        /// 方法（静态），转为大写字符串
        /// </summary>
        /// <param name="strInput">原字符串</param>
        /// <returns>目的字符串</returns>
        public static string ToUpper(string strInput)
        {
            return strInput.ToUpper();
        }
        #endregion

        #region 从字符串中的尾部删除指定的字符串
        /// <summary>
        /// 从字符串中的尾部删除指定的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="removedString"></param>
        /// <returns></returns>
        public static string Remove(string sourceString, string removedString)
        {
            try
            {
                if (sourceString.IndexOf(removedString) < 0)
                    throw new Exception("原字符串中不包含移除字符串！");
                string result = sourceString;
                int lengthOfSourceString = sourceString.Length;
                int lengthOfRemovedString = removedString.Length;
                int startIndex = lengthOfSourceString - lengthOfRemovedString;
                string tempSubString = sourceString.Substring(startIndex);
                if (tempSubString.ToUpper() == removedString.ToUpper())
                {
                    result = sourceString.Remove(startIndex, lengthOfRemovedString);
                }
                return result;
            }
            catch
            {
                return sourceString;
            }
        }
        #endregion

        #region  获取拆分符右边的字符串
        /// <summary>
        /// 获取拆分符右边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        public static string RightSplit(string sourceString, char splitChar)
        {
            string result = null;
            string[] tempString = sourceString.Split(splitChar);
            if (tempString.Length > 0)
            {
                result = tempString[tempString.Length - 1].ToString();
            }
            return result;
        }
        #endregion

        #region 获取拆分符左边的字符串
        /// <summary>
        /// 获取拆分符左边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        public static string LeftSplit(string sourceString, char splitChar)
        {
            string result = null;
            string[] tempString = sourceString.Split(splitChar);
            if (tempString.Length > 0)
            {
                result = tempString[0].ToString();
            }
            return result;
        }
        #endregion

        #region 去掉最后一个逗号
        /// <summary>
        /// 去掉最后一个逗号
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static string DelLastComma(string origin)
        {
            if (origin.IndexOf(",") == -1)
            {
                return origin;
            }
            return origin.Substring(0, origin.LastIndexOf(","));
        }
        #endregion

        #region 删除不可见字符
        /// <summary>
        /// 删除不可见字符
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        public static string DeleteUnVisibleChar(string sourceString)
        {
            string[] aryReg = { "?", "<", ">", "\"\"",">=", "=<",";", "||","/", "|"};
            for (int i = 0; i < aryReg.Length; i++)
            {
                sourceString = sourceString.Replace(aryReg[i], " ");
            }

            sourceString = InputText3(sourceString);

            string StrNohtml = "";
            StrNohtml = System.Text.RegularExpressions.Regex.Replace(sourceString, "<[^>]+>", "");
            StrNohtml = System.Text.RegularExpressions.Regex.Replace(StrNohtml, "&[^;]+;", "");

            return StrNohtml.Trim();

            //sourceString = Regex.Replace(sourceString, @"[\p{P}*]", " ");
            ////sourceString = Regex.Replace(sourceString, @"\s", "");

            //System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
            //for (int i = 0; i < sourceString.Length; i++)
            //{
            //    int Unicode = sourceString[i];
            //    if (Unicode >= 16)
            //    {
            //        sBuilder.Append(sourceString[i].ToString());
            //    }
            //}
            //return sBuilder.ToString();
        }

        /// <summary> 
        /// 过滤sql中非法字符 
        /// </summary> 
        /// <param name="value">要过滤的字符串 </param> 
        /// <returns>string </returns> 
        public static string Filter(string value)
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;
            value = Regex.Replace(value, @";", string.Empty);
            value = Regex.Replace(value, @"'", string.Empty);
            value = Regex.Replace(value, @"&", string.Empty);
            value = Regex.Replace(value, @"%20", string.Empty);
            value = Regex.Replace(value, @"--", string.Empty);
            value = Regex.Replace(value, @"==", string.Empty);
            value = Regex.Replace(value, @" <", string.Empty);
            value = Regex.Replace(value, @">", string.Empty);
            value = Regex.Replace(value, @"%", string.Empty);

            return value;
        }



        /// <summary>
        /// 过滤非法字符
        /// </summary>
        /// <param name="inputString1">等待处理的字符串</param>
        /// <returns></returns>
        public static string InputText3(object inputString1)
        {
            if (inputString1 == null) return "";
            string inputString = inputString1.ToString();
            StringBuilder retVal = new StringBuilder();
            if (inputString != String.Empty)
            {
                inputString = inputString.Trim().ToLower();
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append("");
                            break;
                        case '<':
                            retVal.Append("");
                            break;
                        case '>':
                            retVal.Append("");
                            break;

                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }
                retVal = retVal.Replace("'", "");
                retVal = retVal.Replace(":", "：");
                retVal = retVal.Replace("?", "？");
                retVal = retVal.Replace("*", "");
                //retVal = retVal.Replace(".", "。");
                //retVal = retVal.Replace("/", "、");
                //retVal = retVal.Replace(":", "");
            }

            return retVal.ToString();
        }

       

        #endregion

        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">The string to encode.</param>
        /// <returns>An encoded string.</returns>
        public static string MD5String(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        #endregion

        #region MD5密码判断
        /// <summary>
        /// MD5密码判断.
        /// </summary>
        /// <param name="input">The string to compare.</param>
        /// <param name="hash">The hash to compare against.</param>
        /// <returns>True if the input and the hash are the same, false otherwise.</returns>
        public static bool MD5VerifyString(string input, string hash)
        {
            // Hash the input.
            string hashOfInput = MD5String(input);

            // Create a StringComparer an comare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 得到当前月份 格式2017-07
        public static string GetCurrentMonth()
        {
            DateTime.Now.ToLongDateString().ToString();
            
            return DateTime.Now.ToString("yyyy-MM"); 
        }
        #endregion

    }
}
